package cn.wonderland.house.controller;

import cn.wonderland.house.service.IContractInformationService;
import cn.wonderland.house.domain.ContractInformation;
import cn.wonderland.house.query.ContractInformationQuery;
import cn.wonderland.basic.util.AjaxResult;
import cn.wonderland.basic.util.PageList;
import cn.wonderland.house.service.IHousingInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/contractInformation")
public class ContractInformationController {
    @Autowired
    public IContractInformationService contractInformationService;

    @Autowired
    private IHousingInformationService housingInformationService;


    /**
     * 保存和修改公用的
     *
     * @param contractInformation 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ContractInformation contractInformation) {
        try {
            if (contractInformation.getCtId() != null)
                contractInformationService.update(contractInformation);
            else {
                Long hId = contractInformation.getHId();
                housingInformationService.updateHouseStateById(hId);
                contractInformation.setHouseId(hId);
                contractInformation.setCtRent(contractInformation.getHprice());
                contractInformation.setCtAddress(contractInformation.getHTitlex());
                contractInformationService.add(contractInformation);
            }
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            contractInformationService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public ContractInformation get(@PathVariable("id") Long id) {
        return contractInformationService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<ContractInformation> list() {

        return contractInformationService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<ContractInformation> json(@RequestBody ContractInformationQuery query) {
        return contractInformationService.queryByPage(query);
    }

    /*
     * 批量删除信息
     * */
    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        contractInformationService.batchDelete(ids);
        return AjaxResult.me().success();
    }

}
